package com.zpark.sports.controller;
import com.zpark.sports.service.exception.*;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * 处理全局异常
 * @author xiaoyu
 * @version 1.0 2022/9/27
 */
@ControllerAdvice
public class SportsGlobalExceptionHandlerController {

    @ExceptionHandler(SportsBasicServletException.class)
    public ModelAndView handlerServiceException(Throwable e) {
        //创建视图解析对象
        ModelAndView mv = new ModelAndView();
        if(e instanceof SportsUsernameRegisteredException) { //用户名已经注册异常
            // 设置视图解析
            mv.setViewName("register");
            // 添加绑定数据
            mv.addObject("msg", e.getMessage());
        } else if(e instanceof SportsUserRegException) { //用户注册失败异常
            // 设置视图解析
            mv.setViewName("register");
            // 添加绑定数据
            mv.addObject("msg", e.getMessage());
        } else if(e instanceof SportsUsernameOrPwdIsEmptyException) {
            // 设置视图解析
            mv.setViewName("login");
            // 添加绑定数据
            mv.addObject("msg", e.getMessage());
        } else if(e instanceof SportsWrongPasswordException) {
            // 设置视图解析
            mv.setViewName("login");
            // 添加绑定数据
            mv.addObject("msg", e.getMessage());
        } else if(e instanceof SportsUserNotFoundException) {
            // 设置视图解析
            mv.setViewName("login");
            // 添加绑定数据
            mv.addObject("msg", e.getMessage());
        }
        return mv;
    }
}
